<?php
class Kd_Db_Transaction{

    private $_transactionCount = 0;
    private $_dbDriver = NULL;

    public function __construct(Kd_Db_Driver_Base $driver){
        $this->_dbDriver = $driver;
    }

    public function getDbDriver(){
    	return $this->_dbDriver;
    }

    public function begin(){
        if($this->_transactionCount === 0){
            $this->getDbDriver()->beginTransaction();
        }
        $this->_transactionCount++;
        return $this;
    }

    public function commit(){
        $this->_transactionCount--;
        if($this->_transactionCount === 0){

            $this->getDbDriver()->commitTransaction();
        }
        return $this;
    }

    public function rollback(){
        $this->_transactionCount = -1;
        $this->getDbDriver()->rollbackTransaction();
        return $this;
    }
}